package org.stream.answerduel.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.stream.answerduel.entity.User;

import java.util.List;

/**
 * 功能描述：
 *
 * @Auther: 何俊霖
 * @Date: 2019/7/9 13:02
 */
@Repository
public interface UserRepository extends JpaRepository<User, Long> {

    /**
     * 功能描述：根据昵称查找用户列表
     * @param nickname
     * @return List<User>
     */
    List<User> findByNickName(String nickname);

    /**
     * 功能描述：返回排行前10条数据
     * @author 郭榕
     * @date  2019/7/13 14:03
     * @return List<User>
     */
    @Query(nativeQuery = true , value = "SELECT * from t_user ORDER BY score DESC LIMIT 0,10")
    List<User> findToptenUser();

    /**
     * 功能描述：根据微信id查找用户
     * @author 郭榕
     * @date 2019/7/10  15:00
     * @param wechatid
     * @return User
     */
    User findByWechatID(String wechatid);

    /**
     * 功能描述：查询用户的好友，返回好友列表;
     * @param wechatid
     * @return List<User>
     * @author 郭榕
     * @date   2019/7/11 16:49
     */
    @Query(nativeQuery = true, value = "select C.* from t_user as A join t_friend as B on A.id=B.user_id join t_user as C on B.friend_id = C.id where A.wechat_id = ?1 and B.flag = 1")
    List<User> findFriendsByWechatId(String wechatid);

    /**
     * 功能描述：查询用户的好友，返回好友列表;
     * @param id
     * @return List<User>
     * @author 郭榕
     * @date   2019/7/11 22:16
     */
    @Query(nativeQuery = true, value = "select C.* from t_user as A join t_friend as B on A.id=B.user_id join t_user as C on B.friend_id = C.id where A.id = ?1 and B.flag = 1 ")
    List<User> findFriendsById(long id);

    /**
     * 功能描述 : 查询申请我为好友的用户
     * @param userId
     * @return List<User>
     */
    @Query(nativeQuery = true, value = " SELECT A.* FROM t_user as A JOIN t_friend as B  on A.id = B.user_id where flag = 0 AND friend_id = ?1")
    List<User> findApplyMe(long userId);

}